<?php
namespace bee;
class docLibrary
{ protected static $__libID = "lib_200001010000";
  /**
   *
   * Document library class
   * @var library
   */
  public $library;
  //protected $dispatcher;
  public $sublibraries;
  public $documents;

  protected $__dispatcher;
  protected $__DSDoc;

  public static function userOwnsDoc($user, $document)
  { $perm = new permission();
    $perm_arr = array("create" => NULL, "read" => NULL, "update" => NULL, "delete" => NULL, "execute" => NULL);
    $perm->object_id = $document;
    $perm->object_type = "document";
    $perm->principal_id = $user;
    $perm->principal_type = "user";
    $rsPerm = new rowSet(clone $perm, new afterWhere(" AND '" . date("Y-m-d H:i:s") . "' BETWEEN IFNULL(active_from, '2000-01-01')
    										AND IFNULL(active_to, '2099-12-31')"));
    $allow = NULL;

    while($rsPerm->getRow($perm))
    { //echo $perm->active_from;
      foreach ($perm as $key => $value)
      { if(array_key_exists($key, $perm_arr))
        { if($perm->$key == 1)
          { if($perm_arr[$key] == NULL)
            {
            }
          }
        }
      }

      if($perm->create == 1)
      { if($allow == NULL)
        {  $allow = true;
        }
      } else
      { $allow = false;
      }
    }

    if($allow)
    { $perm = new permission();
      $perm->object_id = $id;
      $perm->object_type = "library";
      $perm->principal_id = app::$user->org_id;
      $perm->principal_type = "org_unit";
      $rsPerm = new rowSet(clone $perm, new afterWhere(" AND '" . date("Y-m.d H:i:s") . "' BETWEEN IFNULL(active_from, '2000-01-01')
    										AND IFNULL(active_to, '2099-12-31')"));
      while($rsPerm->getRow($perm))
      { //print_r($perm);
        if($perm->read == 1)
        { if($allow == NULL)
          {  $allow = true;
          }
        } else
        { $allow = false;
        }
      }
    }

  }

  public function __wakeup()
  { //echo "wup[" . print_r($this) . "]";
    if($this)
    { $this->__DSDoc = XMLDataHub::getInstance();
      $this->__dispatcher = dispatcher::getInstance();
      $this->__dispatcher->addObject($this);//static::$__instance);
    }

  }



  public function __construct($id)
  {

    $this->__DSDoc = XMLDataHub::getInstance();
    $this->__dispatcher = dispatcher::getInstance();
    $this->__dispatcher->addObject($this);
    $this->__dispatcher->dispatch("createDocLibrary");

    $perm = new permission();
    $perm->object_id = $id;
    $perm->object_type = "library";
    $perm->principal_id = app::$user->id;
    $perm->principal_type = "user";
    $rsPerm = new rowSet(clone $perm, new afterWhere(" AND '" . date("Y-m-d H:i:s") . "' BETWEEN IFNULL(active_from, '2000-01-01')
    										AND IFNULL(active_to, '2099-12-31')"));
    $allow = NULL;
    while($rsPerm->getRow($perm))
    { //echo $perm->active_from;
      if($perm->read == 1)
      { if($allow == NULL)
        {  $allow = true;
        }
      } else
      { $allow = false;
      }
    }
    if($allow)
    { $perm = new permission();
      $perm->object_id = $id;
      $perm->object_type = "library";
      $perm->principal_id = app::$user->org_id;
      $perm->principal_type = "org_unit";
      $rsPerm = new rowSet(clone $perm, new afterWhere(" AND '" . date("Y-m.d H:i:s") . "' BETWEEN IFNULL(active_from, '2000-01-01')
    										AND IFNULL(active_to, '2099-12-31')"));
      while($rsPerm->getRow($perm))
      { //print_r($perm);
        if($perm->read == 1)
        { if($allow == NULL)
          {  $allow = true;
          }
        } else
        { $allow = false;
        }
      }

      if($allow)
      { $this->library = new library();
        $this->library->SELECT($id);
        $this->sublibraries = array();
        $sl = new library();
        $sl->parent = $this->library->id;
        $rsSL = new rowSet(clone $sl);
        while($rsSL->getRow($sl))
        { $this->sublibraries[] = clone $sl;
        }

        $this->documents = array();
        $doc = new document();
        $doc->library = $this->library->id;
        $rsDoc = new rowSet(clone $doc);
        while($rsDoc->getRow($doc))
        { $this->documents[] = clone $doc;
        }


      }

    }

  }
  public function onDraw($evt)
  {  $this->__DSDoc->setObject("widgets/tLib", $this);
    //print_r($this);
    //print_r($this->library);
    //print_r($this->sublibraries);

  }

}